// asciitable.cpp : Defines the entry point for the console application.
#include <stdio.h>
#include <tchar.h>
#include <string>
#include <iostream>
#include <vector>

using namespace std;
void JoinChar( char c, int repeat, string& outStr)
{
	for(int i=0; i<repeat; ++i)
	{
		outStr+=c;
	}
	return;
}

void GenerateHorizontalLine(vector<int>& columnWidth, string& outStr1, string& outStr2)
{

	for(vector<int>::iterator i = columnWidth.begin(); i != columnWidth.end(); ++i)
	{
		outStr1+='+';
		JoinChar( '-', *i, outStr1);

		outStr2+='|';
		JoinChar(' ', *i,outStr2);

	}
	outStr1+='+';
	outStr2+='|';
}

void CreateTable( vector<int>& column, vector<int>& row, string& outStr)
{
	string horizontalLine1,horizontalLine2;
	GenerateHorizontalLine(column, horizontalLine1, horizontalLine2);
	for(vector<int>::iterator i= row.begin(); i != row.end(); ++i)
	{
		outStr += (horizontalLine1 +'\n');
		for(int j = 0; j<*i ; ++j)
		{
			outStr += (horizontalLine2 + '\n');
		}
	}
	outStr += (horizontalLine1+'\n');
}

int main(int argc, char* argv[])
{

	if(argc<5)
	{
		cout<<"input was invalided\n";
		cout<<"Try to command asciitable 2 2 2 4\n";
		cout<<"Try to command asciitable 2 2 1 2 3 4\n";
		cout<<"Try to command asciitable 1 5 2 3 4 5 6 7\n";
		return 0;
	}
	int rowNum = atoi(argv[1]);
	int colNum = atoi(argv[2]);

	vector<int> columnWidth;
	vector<int> rowWidth;
	if(argc==5)
	{
		rowWidth    = vector<int>(rowNum, atoi(argv[3]));
		columnWidth = vector<int>(colNum, atoi(argv[4]));
	}
	else if(argc == rowNum+colNum+3)
	{
		char** rowArgv = argv+3;
		char** colArgv = argv+3+rowNum;
		for(int i=0; i<rowNum; ++i)
			rowWidth.push_back( atoi(rowArgv[i]) );
		for(int i=0; i<colNum; ++i)
			columnWidth.push_back( atoi(colArgv[i]) );
	}
	else
	{
		
		cout<<"Try to command asciitable 2 2 2 4\n";
		cout<<"Try to command asciitable 2 2 1 2 3 4\n";
		cout<<"Try to command asciitable 1 5 2 3 4 5 6 7\n";
		system("pause");
		return 0;
	}

	string tblStr;
	CreateTable(columnWidth, rowWidth, tblStr);	
	std::cout<<tblStr;
	system("pause");
	return 0;
}

